package com.llb666.shop.order.api.controller.center;

import com.llb666.common.core.constant.AppConstants;
import com.llb666.common.core.web.response.BaseResponse;
import com.llb666.common.core.web.response.DataResponse;
import com.llb666.shop.order.api.vo.OrderItemsVo;
import com.llb666.shop.order.api.vo.OrderStatusCountsVo;
import com.llb666.shop.order.domain.entity.Orders;
import com.llb666.shop.order.domain.service.center.MyOrdersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户中心-订单接口.
 *
 * @author llb666
 */
@Tag(name = "用户中心-订单接口")
@RestController
@RequestMapping("myorders")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MyOrdersController {

    private final MyOrdersService myOrdersService;

    @Operation(summary = "获得订单状态数概况")
    @PostMapping("/statusCounts")
    public BaseResponse statusCounts(@Parameter(name = "userId", description = "用户id", required = true)
                                     @RequestParam String userId) {
        OrderStatusCountsVo result = myOrdersService.getOrderStatusCounts(userId);
        return DataResponse.ok(result);
    }

    @Operation(summary = "查询订单列表")
    @PostMapping("/query")
    public BaseResponse query(
            @Parameter(name = "userId", description = "用户id", required = true) @RequestParam String userId,
            @Parameter(name = "orderStatus", description = "订单状态") @RequestParam(required = false) Integer orderStatus,
            @Parameter(name = "page", description = "查询下一页的第几页") Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数") Integer pageSize) {
        if (page == null) {
            page = AppConstants.COMMON_PAGE;
        }
        if (pageSize == null) {
            pageSize = AppConstants.COMMON_PAGE_SIZE;
        }
        return DataResponse.ok(myOrdersService.queryMyOrders(userId, orderStatus, page, pageSize));
    }

    @Operation(summary = "商家发货")
    @GetMapping("/deliver")
    public BaseResponse deliver(@Parameter(name = "orderId", description = "订单id", required = true)
                                @RequestParam String orderId) {
        myOrdersService.updateDeliverOrderStatus(orderId);
        return BaseResponse.ok();
    }

    @Operation(summary = "用户确认收货")
    @PostMapping("/confirmReceive")
    public BaseResponse confirmReceive(
            @Parameter(name = "orderId", description = "订单id", required = true) @RequestParam String orderId,
            @Parameter(name = "userId", description = "用户id", required = true) @RequestParam String userId) {
        // 验证用户和订单是否有关联关系，避免非法用户调用.
        myOrdersService.checkUserOrder(userId, orderId);
        boolean res = myOrdersService.updateReceiveOrderStatus(orderId);
        if (!res) {
            return DataResponse.errorMsg("订单确认收货失败！");
        }
        return BaseResponse.ok();
    }

    @Operation(summary = "用户删除订单")
    @PostMapping("/delete")
    public BaseResponse delete(
            @Parameter(name = "orderId", description = "订单id", required = true) @RequestParam String orderId,
            @Parameter(name = "userId", description = "用户id", required = true) @RequestParam String userId) {
        // 验证用户和订单是否有关联关系，避免非法用户调用.
        myOrdersService.checkUserOrder(userId, orderId);
        boolean res = myOrdersService.deleteOrder(userId, orderId);
        if (!res) {
            return DataResponse.errorMsg("订单删除失败！");
        }
        return BaseResponse.ok();
    }

    @Operation(summary = "查询订单动向")
    @PostMapping("/trend")
    public BaseResponse trend(
            @Parameter(name = "userId", description = "用户id", required = true) @RequestParam String userId,
            @Parameter(name = "page", description = "查询下一页的第几页") Integer page,
            @Parameter(name = "pageSize", description = "分页的每一页显示的条数") Integer pageSize) {
        if (page == null) {
            page = AppConstants.COMMON_PAGE;
        }
        if (pageSize == null) {
            pageSize = AppConstants.COMMON_PAGE_SIZE;
        }
        return DataResponse.ok(myOrdersService.getOrdersTrend(userId, page, pageSize));
    }


    @Operation(summary = "查询订单动向")
    @PostMapping("/pending")
    public BaseResponse pending(
            @Parameter(name = "userId", description = "用户id", required = true) @RequestParam String userId,
            @Parameter(name = "orderId", description = "订单id", required = true) @RequestParam String orderId) {

        // 判断用户和订单是否关联
        myOrdersService.checkUserOrder(userId, orderId);
        List<OrderItemsVo> list = OrderItemsVo.Converter.INSTANCE.do2VoList(
                myOrdersService.queryPendingComment(orderId));
        return DataResponse.ok(list);
    }

    /***************************     以下接口为feign远程调用               ********************/

    @Operation(summary = "Client:查询用户订单，验证订单真实性.")
    @GetMapping("/details")
    public Orders details(
            @Parameter(name = "orderId", description = "订单id", required = true) @RequestParam String orderId,
            @Parameter(name = "userId", description = "用户id", required = true) @RequestParam String userId) {
        // 验证用户和订单是否有关联关系，避免非法用户调用.
        return myOrdersService.details(userId, orderId);
    }

    @Operation(summary = "Client:更新订单为'已评价'")
    @PutMapping("/updateCommentsStatus")
    public void updateCommentsStatus(@Parameter(name = "orderId", description = "订单id", required = true)
                                     @RequestParam String orderId) {
        myOrdersService.updateCommentsStatus(orderId);
    }

    @Operation(summary = "Client:修改订单状态表的留言时间")
    @PutMapping("/updateOrderStatus")
    public void updateOrderStatus(@Parameter(name = "orderId", description = "订单id", required = true)
                                  @RequestParam String orderId) {
        myOrdersService.updateOrderStatus(orderId);
    }

}
